

#include "config_network.h"
#include "pt_fs.h"
#include "first_title.h"

ui_config_network_widget_t u_cfg_net = {
    .loading_img_count= 0
};

void config_network_loading_img_cb(lv_timer_t * t)
{
    u_cfg_net.loading_img_count++;    
    if(u_cfg_net.loading_img_count > 17){
        u_cfg_net.loading_img_count = 0;
    }

    lv_img_set_src(u_cfg_net.img_loading,loading_img[u_cfg_net.loading_img_count]);
    //********************************************************** */
#if 1 //for test
    static int test_cnt = 0;
    test_cnt++;
    if(test_cnt > 18000) //15minute : 18000 = 50 ms x 20 x 60 x15
    {
        test_cnt = 0;
        if(u_cfg_net.img_loading_timer){
            lv_timer_del(u_cfg_net.img_loading_timer);
            u_cfg_net.img_loading_timer = NULL;
        }

        debug_d("cfg net err\n");
        
        //page_pop_anim(LOAD_ANIM_MOVE_LEFT);

        page_install("link_fail",lv_page_linked_network_fail_create(),NULL);
        page_push_anim("link_fail",LOAD_ANIM_MOVE_LEFT);
    }else
    {
        uint8_t token_stat = pt_accredit_token_get();
        //debug_d("pt_accredit_token_get:%d\n",token_stat);
        if(token_stat)
        {
            if(test_cnt > 34)
            {
                test_cnt = 0;
                if(u_cfg_net.img_loading_timer){
                    lv_timer_del(u_cfg_net.img_loading_timer);
                    u_cfg_net.img_loading_timer = NULL;
                }

                debug_d("cfg net succeed\n");

                page_install("link_net",lv_page_linked_network_create(),NULL);
                page_push_anim("link_net",LOAD_ANIM_MOVE_LEFT);

                // page_install("author",lv_page_network_create());
                // page_push_anim("author",LOAD_ANIM_MOVE_LEFT);

            }

        }

    }

#endif
    //********************************************************** */
    
}


void ui_init_config_network_page_src(lv_obj_t *superview)
{
    first_title_set( FIRST_PAGE_CONFIG_NET, 0, 3);

    u_cfg_net.img_loading = lv_img_create(superview);
    lv_img_set_src( u_cfg_net.img_loading, loading_img[0]);
    lv_obj_set_pos(u_cfg_net.img_loading,196,111);
  
    lv_obj_t * label = lv_label_create(superview);
    lv_obj_set_style_text_font( label, lv_font_get(20), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(label,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN);
    lv_obj_set_style_text_opa(label,LV_OPA_80,0);
    lv_label_set_text( label, "正在配网中...");
    lv_obj_set_pos(label,167,176);

    u_cfg_net.img_loading_timer = lv_timer_create(config_network_loading_img_cb, 50, NULL);

}

void ui_release_config_network_page_src(lv_obj_t *superview)
{
    if(u_cfg_net.img_loading)
    {
        lv_obj_del(u_cfg_net.img_loading);
        u_cfg_net.img_loading = NULL;
    }

    if(u_cfg_net.img_loading_timer)
    {
        lv_timer_del(u_cfg_net.img_loading_timer);
        u_cfg_net.img_loading_timer = NULL;
        u_cfg_net.loading_img_count = 0;
    }
}

static void network_page_on_custom_attr_config(page_base_t* self)
{
    return ;
}
static void network_page_on_view_load(page_base_t* self)
{
    ui_init_config_network_page_src(self->root);
    return ;
}
static void network_page_on_view_did_load(page_base_t* self)
{
    return ;
}
static void network_page_on_view_will_appear(page_base_t* self)
{
    return ;
}
static void network_page_on_view_did_appear(page_base_t* self)
{
    return ;
}
static void network_page_on_view_will_disappear(page_base_t* self)
{
    return ;
}
static void network_page_on_view_did_disappear(page_base_t* self)
{
    return ;
}
static void network_page_on_view_did_unload(page_base_t* self)
{
    ui_release_config_network_page_src(self->root);
    return ;
}

static page_vtable_t page_param = {
    .on_custom_attr_config  = network_page_on_custom_attr_config,
    .on_view_load           = network_page_on_view_load,
    .on_view_did_load       = network_page_on_view_did_load,
    .on_view_will_appear    = network_page_on_view_will_appear,
    .on_view_did_appear     = network_page_on_view_did_appear,
    .on_view_will_disappear = network_page_on_view_will_disappear,
    .on_view_did_disappear  = network_page_on_view_did_disappear,
    .on_view_did_unload     = network_page_on_view_did_unload,
};


page_vtable_t* lv_page_config_network_create(void)
{
    return &page_param;
}
